package com.cn.lp.export.client.entity;

import com.cn.lp.export.client.controller.ExportRequestDTO;
import com.cn.lp.export.client.controller.ExportResponseDTO;
import com.cn.lp.export.client.utils.JSONAide;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

/**
 * Description: 请描述你的文件
 *
 * @author wuqirong
 * @date 2020-10-20
 * <p>
 * All rights Reserved, Designed www.xiao100.com
 */
@Service
public class ExportService {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    public ExportResponseDTO export(ExportRequestDTO request) {
        //业务处理
        BaseExportInfoParam.ExportEventVo exportEventVo = new BaseExportInfoParam.ExportEventVo();
        exportEventVo.setBusinessType(request.getBusinessType())
            .setCondition(JSONAide.toObject(JSONAide.toJson(request.getCondition()), request.getBusinessType().getConditionClassType()));
        ExportHandleEvent exportHandleEvent = request.getBusinessType().getFactory().create(exportEventVo);
        applicationEventPublisher.publishEvent(exportHandleEvent);
        BaseExportInfoParam.ExportEventResultVo resultVo = exportHandleEvent.getResult();
        ExportResponseDTO dto = new ExportResponseDTO();
        dto.setResultUrl(resultVo.getResultUrl());
        dto.setTaskId(String.valueOf(resultVo.getTaskId()));
        return dto;
    }

}
